#include "../Base/stdafx.h"
#include "../Base/GameEngine.h"
#include "../Base/DXDevice.h"
#include "EffectLib.h"
#include "Effect.h"
#include "../Base/DirectInput.h"

Effect::Effect(const tstring& file): m_Effect(0)
{
	m_Effect = EFFECTLIB->CreateEffect(file);
	
	if(m_Effect == NULL)
	{
		MessageBox(NULL, _T("Kan effect niet maken. Kijk in het debugvenster."), NULL, MB_OK);
		PostMessage(GAME_ENGINE->GetWindow(), WM_DESTROY, NULL, NULL);
	}else{
		m_Techniek = m_Effect->GetTechniqueByName("Render");

		m_WorldVar = m_Effect->GetVariableByName("gWorld")->AsMatrix();
		m_WVPVar = m_Effect->GetVariableByName("gWVP")->AsMatrix();
		m_TextureVar = m_Effect->GetVariableByName("diffuseMap")->AsShaderResource();
		m_ShadowMapVar = m_Effect->GetVariableByName("ShadowMap")->AsShaderResource();
		m_LightWVPVar = m_Effect->GetVariableByName("gLightWVP")->AsMatrix();
		m_LightPos = m_Effect->GetVariableByName("gLightPos")->AsVector();
		m_LightColor = m_Effect->GetVariableByName("gLightColor")->AsVector();
		m_LightSpec = m_Effect->GetVariableByName("gLightSpec")->AsVector();
		m_LightAmbient = m_Effect->GetVariableByName("gLightAmbient")->AsVector();
		m_EyePosVar = m_Effect->GetVariableByName("gEyePosW")->AsVector();
		m_TexMatVar = m_Effect->GetVariableByName("gTexMat")->AsMatrix();
	}
}

Effect::~Effect()
{

}

void Effect::SetMatrixen(const D3DXMATRIX& world, const D3DXMATRIX& view, const D3DXMATRIX& proj)
{
	m_WorldVar->SetMatrix((float*)&world);
	m_WVPVar->SetMatrix((float*)&(world*view*proj));
}

void Effect::SetTexture(const ID3D10ShaderResourceView* texture)
{
	m_TextureVar->SetResource(const_cast<ID3D10ShaderResourceView*>(texture));
}

ID3D10EffectTechnique* Effect::GetTech() const
{
	return m_Techniek;
}

void Effect::SetLightWVP( const D3DXMATRIX& lightWVP )
{
	m_LightWVPVar->SetMatrix((float*)&lightWVP);
}

void Effect::ResetShader()
{
	m_TextureVar->SetResource(NULL);
	m_ShadowMapVar->SetResource(NULL);
}

void Effect::SetLight( const D3DXVECTOR3& pos, const D3DXVECTOR4& color, const D3DXVECTOR4& spec, const D3DXVECTOR4& ambient )
{
	m_LightPos->SetFloatVector((float*)&(const_cast<D3DXVECTOR3&>(pos)));
	m_LightColor->SetFloatVector((float*)&(const_cast<D3DXVECTOR4&>(color)));
	m_LightSpec->SetFloatVector((float*)&(const_cast<D3DXVECTOR4&>(spec)));
	m_LightAmbient->SetFloatVector((float*)&(const_cast<D3DXVECTOR4&>(ambient)));
}

void Effect::SetShadowMap(const ID3D10ShaderResourceView* shadowMap)
{
	m_ShadowMapVar->SetResource(const_cast<ID3D10ShaderResourceView*>(shadowMap));
}

void Effect::SetEyePos( const D3DXVECTOR3& eyePos )
{
	m_EyePosVar->SetFloatVector((float*)&(const_cast<D3DXVECTOR3&>(eyePos)));
}

void Effect::SetTexScale( float rows, float cols )
{
	D3DXMATRIX temp;
	D3DXMatrixScaling(&temp, rows, cols, 1);
	m_TexMatVar->SetMatrix((float*)&temp);
}